<?php
import("@.Validator.UserValidator");
import("@.Validator.OrganizationValidator");
import("@.Service.UserService");

class UserAction extends Action
{
    //错误代码
    const USER_VALIDATE_FAILED = 1;//校验用户相关字段失败
    const ORG_VALIDATE_FAILED = 2;//校验组织相关字段失败


    const LOG_PREFIX = "UserAction->";

    public function __construct()
    {
        $this->userService = UserService::getInstance();
    }

    private $userService;

    //注册用户
    public function createUser($tel = '', $psd = '', $orgName = '', $orgType = '', $orgDesc = '')
    {
        Log::write(self::LOG_PREFIX . 'createUser() start, $tel:' . $tel . ' $psd:' . $psd . ' $orgName:' . $orgName . ' $orgType:' . $orgType . ' $orgDesc:' . $orgDesc, Log::INFO);
        $user = array("tel" => $tel, "psd" => $psd);
        $User = new UserModel();
        //用户相关字段校验
        if (!$User->validate(UserValidator::getRegisterRules())->create($user)) {
            Log::write(self::LOG_PREFIX . 'createUser()  $User->validate()->create() fail.', Log::INFO);
            $this->ajaxReturn(output_json_fail(self::USER_VALIDATE_FAILED));//code=1,用户注册手机号及密码时校验失败
        }
        Log::write(self::LOG_PREFIX . 'createUser() $User->validate()->create():pass', Log::INFO);
        $org = array("orgType" => $orgType, "orgDesc" => $orgDesc, "orgName" => $orgName);
        $Org = new OrganizationModel();
        //组织相关字段校验
        if (!$Org->validate(OrganizationValidator::getCreateOrgRules())->create($org)) {
            Log::write(self::LOG_PREFIX . 'createUser()  $Org->validate()->create() fail.', Log::INFO);
            $this->ajaxReturn(output_json_fail(self::ORG_VALIDATE_FAILED));//code=2,注册时组织信息校验失败
        }
        Log::write('UserAction createUser() $Org->validate()->create():pass', Log::INFO);
        //把service需要的Model放在一个数组里传给对应的service
        $params = array("User" => $User, "Org" => $Org);
        $result = $this->userService->createUser($params);
        if (is_array($result)) {
            Log::write(self::LOG_PREFIX . 'createUser() end, success.', Log::INFO);
            $this->ajaxReturn(output_json_success($result));
        } else {
            Log::write(self::LOG_PREFIX . 'createUser() end, fail.', Log::INFO);
            $this->ajaxReturn(output_json_fail($result));
        }

    }

    public function login($acctNo = '', $psd = '')
    {
        Log::write(self::LOG_PREFIX . 'login() start, $acctNo:' . $acctNo . ' $psd:' . $psd, Log::INFO);
        $user = array("acctNo" => $acctNo, "psd" => $psd);
        $User = new UserModel();
        //用户账户相关字段校验
        if (!$User->validate(UserValidator::getLoginRules())->create($user)) {
            Log::write(self::LOG_PREFIX . 'login() $User->validate()->create() fail.', Log::INFO);
            $this->ajaxReturn(output_json_fail(self::USER_VALIDATE_FAILED));//code=1,用户登录时账户号及密码校验失败
        }
        Log::write(self::LOG_PREFIX . 'login() $User->validate()->create():pass', Log::INFO);
        $params = array("user" => $user);
        $result = $this->userService->login($params);
        if (is_array($result)) {
            Log::write(self::LOG_PREFIX . 'login() end, success.', Log::INFO);
            $this->ajaxReturn(output_json_success($result));
        } else {
            Log::write(self::LOG_PREFIX . 'login()  end, fail.', Log::INFO);
            $this->ajaxReturn(output_json_fail($result));
        }
    }

}


?>